import axios from 'axios'
import config from './config';
import Qs from 'qs'

let instance = axios.create({
    timeout: 5000,
    baseURL: config.SERVER_BASEPATH
})

//请求拦截
instance.interceptors.request.use(config=>{

    return config
},err=>{

    return Promise.reject(err);
})

//响应拦截
instance.interceptors.response.use(res=>{

    return res
},err=>{

    return Promise.reject(err);
})

/**
 * 请求的方法
 * @param {*} config 请求的配置信息，config.method请求方法，config.params请求参数，config.url请求路由地址
 */
async function http({method = 'get',url,params}){
    let result ;
    if(method === 'get' || method === 'delete'){
        await instance[method](url,{
            params
        }).then(res=>{
            result = res.data
        }).catch(err=>{
            result = err
        })
    }else if(method === 'post' || method === 'put' || method === 'patch'){
	if(url=='/admin/login'){




axios({
    url: config.SERVER_BASEPATH+'/admin/login',
    method: 'post',
    transformRequest: [function (data) {
        // 对 data 进行任意转换处理
        return Qs.stringify(data)
    }],
    data: {
        username:params.username,
        password:params.password
    }

}).then(res=>{
  console.log(res)
            result = res.data
        }).catch(err=>{
            result = err
        })


	}else{

      await instance[method](url,params).then(res=>{
            result = res.data
        }).catch(err=>{
            result = err
        })

	}







    }
    return result
}

export default http
